﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.IO.Compression
{
    public static class ZipFileExtensions
    {
        public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName, bool overwrite, Func<string, string, bool> skipExtract = null)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (destinationDirectoryName == null)
            {
                throw new ArgumentNullException("destinationDirectoryName");
            }

            DirectoryInfo directoryInfo = Directory.CreateDirectory(destinationDirectoryName);
            string text = directoryInfo.FullName;

            foreach (ZipArchiveEntry zipArchiveEntry in source.Entries)
            {
                string fullPath = Path.GetFullPath(Path.Combine(text, zipArchiveEntry.FullName));
                bool? flag = skipExtract?.Invoke(fullPath, zipArchiveEntry.FullName);
                if (flag.HasValue && flag.Value)
                {
                    continue;
                }

                if (Path.GetFileName(fullPath).Length == 0)
                {
                    if (zipArchiveEntry.Length != 0L)
                    {
                        throw new IOException("IO_DirectoryNameWithData");
                    }
                    Directory.CreateDirectory(fullPath);
                }
                else
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
                    zipArchiveEntry.ExtractToFile(fullPath, overwrite);
                }
            }
        }
    }
}
